package com.andnnl.template;

import java.util.function.BiFunction;

/**
 * Created by chenss on 2024/8/27.
 */
public class KeyWord {
    String keyword;// 关键字
    int lever;// 优先级，越大越优先
    int caseSensitivity;// 大小写匹配，1:区分大小写，0:忽略大小写
    boolean isFullCompare = false;// 完全匹配
    boolean isStartWith = false;// 前缀匹配
    Object data;// 附加额外的数据

    public static final BiFunction<String, KeyWord, Boolean> MatchFunc = (originalText, kw) -> {
        // 完全匹配
        if (kw.isFullCompare()) {
            if (kw.getCaseSensitivity() == 1) {
                return kw.getKeyword().equals(originalText);// 全量加大小写匹配
            }
            return kw.getKeyword().equalsIgnoreCase(originalText);// 全量忽略大小写
        }
        // 前缀匹配
        if (kw.isStartWith()) {
            if (kw.getCaseSensitivity() == 1) {
                return originalText.startsWith(kw.getKeyword());// 区分大小写
            }
            String keywordLower = kw.getKeyword().toLowerCase();
            int keywordLength = keywordLower.length();
            if (originalText.length() < keywordLength) {
                return false;
            }
            return originalText.substring(0, keywordLength).toLowerCase().equals(keywordLower);
        }
        // 包含匹配
        if (kw.getCaseSensitivity() == 1) {
            return originalText.indexOf(kw.getKeyword()) != -1;
        }
        return true;
    };

    public KeyWord(String keyword, int lever, int caseSensitivity, boolean isFullCompare, boolean isStartWith) {
        this.keyword = keyword;
        this.lever = lever;
        this.caseSensitivity = caseSensitivity;
        this.isFullCompare = isFullCompare;
        this.isStartWith = isStartWith;
    }

    public KeyWord(String keyword, int lever, int caseSensitivity, boolean isFullCompare, boolean isStartWith, Object data) {
        this(keyword, lever, caseSensitivity, isFullCompare, isStartWith);
        this.data = data;
    }

    public KeyWord(String keyword, int lever, int caseSensitivity, boolean isFullCompare) {
        this.keyword = keyword;
        this.lever = lever;
        this.caseSensitivity = caseSensitivity;
        this.isFullCompare = isFullCompare;
    }

    public KeyWord(String keyword, int lever, int caseSensitivity, boolean isFullCompare, Object data) {
        this(keyword, lever, caseSensitivity, isFullCompare);
        this.data = data;
    }

    public KeyWord(String keyword, int lever, int caseSensitivity) {
        this.keyword = keyword;
        this.lever = lever;
        this.caseSensitivity = caseSensitivity;
    }

    public KeyWord(String keyword, int lever, int caseSensitivity, Object data) {
        this(keyword, lever, caseSensitivity);
        this.data = data;
    }

    public KeyWord(String keyword, int lever) {
        this.keyword = keyword;
        this.lever = lever;
    }

    public KeyWord(String keyword, int lever, Object data) {
        this(keyword, lever);
        this.data = data;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public int getLever() {
        return lever;
    }

    public void setLever(int lever) {
        this.lever = lever;
    }

    public int getCaseSensitivity() {
        return caseSensitivity;
    }

    public void setCaseSensitivity(int caseSensitivity) {
        this.caseSensitivity = caseSensitivity;
    }

    public boolean isFullCompare() {
        return isFullCompare;
    }

    public void setFullCompare(boolean fullCompare) {
        isFullCompare = fullCompare;
    }

    public boolean isStartWith() {
        return isStartWith;
    }

    public void setStartWith(boolean startWith) {
        isStartWith = startWith;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "KeyWord{" +
                "keyword='" + keyword + '\'' +
                ", lever=" + lever +
                ", caseSensitivity=" + caseSensitivity +
                ", isFullCompare=" + isFullCompare +
                ", isStartWith=" + isStartWith +
                ", data=" + data +
                '}';
    }
}
